package edu.faytechcc.csc289.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * Builds the main JFrame to hold all items and function as the control
 *
 * 03/2014 CSC 289
 *
 * @author William Robles
 */
public class MainFrame extends JFrame
{

    private MainFrame managerSalesFrame;
    private MainFrame managerInventoryFrame;
    private SalesView sV;
    private LoginView lV;
    private ManagementMenu mM;
    private InventoryView iV;

    private JPanel logoPanel;
    private ImageIcon logo;
    private JLabel logoLabel;

    private String position = null;

    private final ActionListener bL;

    public MainFrame()
    {     
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.setUndecorated(true);

        buildLogo();
        bL = new ButtonListener();
        lV = new LoginView(bL);
        this.add(logoPanel, BorderLayout.NORTH);
        this.add(lV, BorderLayout.CENTER);

        logoPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
        lV.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
        pack();
        this.setLocationRelativeTo(null);

        setVisible(true);

    }

    /**
     * buildLogo method creates the logo
     */
    private void buildLogo()
    {   
        logoPanel = new JPanel();

        logo = new ImageIcon("res/loginLogo.png");
        logoLabel = new JLabel(logo);

        logoPanel.add(logoLabel);
    }

    /**
     * setModule method removes current panel in mainframe and places
     * a new panel
     * @param panel to be displayed
     */
    public void setModule(JPanel panel)
    {
        JPanel temp = (JPanel) this.getContentPane().getComponent(1);
        this.remove(temp);
        this.add(panel, BorderLayout.CENTER);
        this.validate();
        this.repaint();
        this.pack();
        this.setLocationRelativeTo(null);
    }
    
    /**
     * launchSales method creates and displays the sales panel either in
     * a new frame or in the mainframe depending on position
     */
    private void launchSales()
    {
        sV = new SalesView();

        if ((managerSalesFrame == null || !managerSalesFrame.isShowing())
                && position.equals("Owner") || position.equals("Manager"))
        {
            managerSalesFrame = new MainFrame();
            managerSalesFrame.addMouseListener(new MouseActions());
            managerSalesFrame.addMouseMotionListener(new MouseActions());
            managerSalesFrame.setModule(sV);
        }
        else if(position.equals("Sales Associate"))
        {
            setModule(sV);
        }
    }
    
    /**
     * launchManagerMenu methods creates and displays the management panel
     */
    private void launchManagerMenu()
    {     
        mM = new ManagementMenu(bL);
        setModule(mM);
    }
    
    /**
     * launchInventory method creates and displays the inventory panel
     * and checks if it is already up.
     */
    private void launchInventory()
    {
        iV = new InventoryView();
        if (managerInventoryFrame == null || !managerInventoryFrame.isShowing())
        {
            managerInventoryFrame = new MainFrame();
            managerInventoryFrame.addMouseListener(new MouseActions());
            managerInventoryFrame.addMouseMotionListener(new MouseActions());
            managerInventoryFrame.setModule(iV);
        }
    }
    

    /**
     * ButtonListener class for entire program
     */
    private class ButtonListener implements ActionListener
    {

        public ButtonListener()
        {
        }

        @Override
        public void actionPerformed(ActionEvent ae)
        {
            if (ae.getActionCommand().equals("OK") && position == null)
            {
                lV.getEmployees();

                if (lV.validateLogin())
                {
                    position = lV.getPosition();
                    switch (position)
                    {
                        case "Owner":
                            launchManagerMenu();
                            break;
                        case "Sales Associate":
                            launchSales();
                            break;
                        case "Manager":
                            launchManagerMenu();
                            break;
                    }
                }
            }
            else if(ae.getActionCommand().equals("Sales"))
            {
                launchSales();
            }
            else if(ae.getActionCommand().equals("Inventory Management"))
            {
                launchInventory();
            }
            else if(ae.getActionCommand().equals("Exit"))
            {
                System.exit(0);
            }

        }
    }
}
